#include <iostream>
#include <stack>
using namespace std;
class Solution {
public:
    int calculate(string s) {
        int num = 0;
        stack<int> stk;
        char pre = '+';
        int n = s.length();
        for (int i = 0; i < n; i++) {
            if (isdigit(s[i])) {
                num = num * 10 + (s[i] - '0');
            }
            if ((!isdigit(s[i]) && s[i] != ' ') || i == n - 1) {
                if (pre == '+') {
                    stk.push(num);
                }
                else if (pre == '-') {
                    stk.push(-num);
                }
                else if (pre == '*') {
                    int top = stk.top();
                    stk.pop();
                    stk.push(top * num);
                }
                else if (pre == '/') {
                    int top = stk.top();
                    stk.pop();
                    stk.push(top / num);
                }
                pre = s[i];
                num = 0;
            }
        }

        int result = 0;
        while (!stk.empty()) {
            result += stk.top();
            stk.pop();
        }
        return result;
    }
};